package org.component 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.SimpleButton;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	[Event(name = "change", type = "flash.events.Event")]
	
	/**
	 * ...
	 * @author kevin
	 */
	public class Hslider extends EventDispatcher
	{
		//需要鼠标效果的显示对象
		private const LIST:Array = ["scrollLeft", "scrollRight"];
		
		private var _sliderView:MovieClip = null;
		private var _scrollLeft:SimpleButton = null;	// 向左滚动按钮
		private var _scrollRight:SimpleButton = null;	// 向右滚动按钮
		
		private var _currentBtn:SimpleButton = null;	// 当前按钮
		
		// 基本属性
		private var _pageCount:int = 0;					// 单页行数
		private var _maxCount:int = 0;					// 最大行数
		private var _speed:Number = 1;					// 滚动速度
		
		private var _itemIndex:int = 0;					//item当前索引值
		
		public function Hslider(sliderView:MovieClip) 
		{
			_sliderView = sliderView;
			_scrollLeft = sliderView.scrollLeft;
			_scrollRight = sliderView.scrollRight;
		}
		
		public function update(pageCount:Number, maxCount:Number, itemIndex:int = 0):void		
		{
			_pageCount = pageCount;
			_maxCount = maxCount;
			_itemIndex = itemIndex;
						
			if (_itemIndex > (_maxCount - _pageCount) || _itemIndex < 0 || _maxCount <= _pageCount)
			{
				removeEvents();
			}				
			else
			{
				addEvents();
			}
		}
		
		/**
		 * 添加事件
		 */
		private function addEvents():void
		{
			_scrollLeft.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);			
			_scrollRight.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);			
		}
		
		/**
		 * 移除事件
		 */
		private function removeEvents():void
		{
			_scrollLeft.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);			
			_scrollRight.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);	
		}
		
		private function downHandler(e:MouseEvent):void
		{
			if (e.currentTarget == _scrollLeft)
			{
				_speed = -Math.abs(_speed);				
				_currentBtn = _scrollLeft;
			}
			else
			{
				_speed = Math.abs(_speed);				
				_currentBtn = _scrollRight;
			}
			
			_sliderView.addEventListener(Event.ENTER_FRAME, scrollHandler);
			_currentBtn.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		
		private function scrollHandler(e:Event):void 
		{			
			_itemIndex += _speed;			
			
			adjustPosition();
			
			dispatchEvent(new Event(Event.CHANGE));			// 通知外部接收事件
		}
		
		private function upHandler(e:MouseEvent):void 
		{
			_sliderView.removeEventListener(Event.ENTER_FRAME, scrollHandler);
			e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, upHandler);
		}
		
		/**
		 * 检查索引位置
		 * @return
		 */
		private function adjustPosition():Boolean
		{
			if (_itemIndex <= 0)
			{
				_itemIndex = 0;
				return false;
			}
			
			if (_itemIndex >= (_maxCount - _pageCount))
			{
				_itemIndex = _maxCount - _pageCount;
				return false;
			}			
			return true;			
		}
		
		public function get itemIndex():int { return _itemIndex; }
	}

}